/* Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "zobjects_gtk.h"

static GdkMonitor *gdk_get_n_monitor( struct ZlangRuntime *rt , int no )
{
	GdkDisplay					*display = NULL ;
	GListModel					*list_model = NULL ;
	GdkMonitor					*monitor = NULL ;

	display = gdk_display_get_default() ;
	if( display == NULL )
		return NULL;

	list_model = gdk_display_get_monitors( display ) ;
	if( list_model == NULL )
		return NULL;

	monitor = g_list_model_get_item( list_model , no ) ;

	return monitor;
}

ZlangInvokeFunction ZlangInvokeFunction_guimonitor_GetScreenWidth;
int ZlangInvokeFunction_guimonitor_GetScreenWidth( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangObject				*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	GdkMonitor					*monitor = NULL ;
	GdkRectangle					geometry ;
	int32_t						width ;

	monitor = gdk_get_n_monitor( rt , 0 ) ;
	if( monitor == NULL )
	{
		UnreferObject( rt , out1 );
		return ThrowErrorException( rt , ZLANG_ERROR_INTERNAL , EXCEPTION_MESSAGE_INTERNAL_ERROR );
	}

	gdk_monitor_get_geometry( monitor , & geometry );
	g_object_unref( monitor );
	width = geometry.width ;

	CallRuntimeFunction_int_SetIntValue( rt , out1 , width );

	return 0;
}

ZlangInvokeFunction ZlangInvokeFunction_guimonitor_GetScreenHeight;
int ZlangInvokeFunction_guimonitor_GetScreenHeight( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangObject				*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	GdkMonitor					*monitor = NULL ;
	GdkRectangle					geometry ;
	int32_t						height ;

	monitor = gdk_get_n_monitor( rt , 0 ) ;
	if( monitor == NULL )
	{
		UnreferObject( rt , out1 );
		return ThrowErrorException( rt , ZLANG_ERROR_INTERNAL , EXCEPTION_MESSAGE_INTERNAL_ERROR );
	}

	gdk_monitor_get_geometry( monitor , & geometry );
	g_object_unref( monitor );
	height = geometry.height ;

	CallRuntimeFunction_int_SetIntValue( rt , out1 , height );

	return 0;
}

#if 0
ZlangInvokeFunction ZlangInvokeFunction_guimonitor_GetWorkareaWidth;
int ZlangInvokeFunction_guimonitor_GetWorkareaWidth( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_guimonitor		*guimonitor_direct_prop = GetObjectDirectProperty(obj) ;
	struct ZlangObject				*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	GdkMonitor					*monitor = NULL ;
	GdkRectangle					workarea ;
	int32_t						width ;

	monitor = gdk_get_n_monitor( rt , 0 ) ;
	if( monitor == NULL )
	{
		UnreferObject( rt , out1 );
		return ThrowErrorException( rt , ZLANG_ERROR_INTERNAL , EXCEPTION_MESSAGE_INTERNAL_ERROR );
	}

	gdk_monitor_get_workarea( monitor , & workarea );
	g_object_unref( monitor );
	width = workarea.width ;

	CallRuntimeFunction_int_SetIntValue( rt , out1 , width );

	return 0;
}
#endif

ZlangCreateDirectPropertyFunction ZlangCreateDirectProperty_guimonitor;
void *ZlangCreateDirectProperty_guimonitor( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_guimonitor		*guimonitor_prop = NULL ;
	
	guimonitor_prop = (struct ZlangDirectProperty_guimonitor *)ZLMALLOC( sizeof(struct ZlangDirectProperty_guimonitor) ) ;
	if( guimonitor_prop == NULL )
	{
		SET_RUNTIME_ERROR( rt , RUNTIME_ERROR , ZLANG_ERROR_ALLOC , "alloc memory for entity" )
		return NULL;
	}
	memset( guimonitor_prop , 0x00 , sizeof(struct ZlangDirectProperty_guimonitor) );

	return guimonitor_prop;
}

ZlangDestroyDirectPropertyFunction ZlangDestroyDirectProperty_guimonitor;
void ZlangDestroyDirectProperty_guimonitor( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_guimonitor	*guimonitor_direct_prop = GetObjectDirectProperty(obj) ;
	
	ZLFREE( guimonitor_direct_prop );
	
	return;
}

ZlangSummarizeDirectPropertySizeFunction ZlangSummarizeDirectPropertySize_guimonitor;
void ZlangSummarizeDirectPropertySize_guimonitor( struct ZlangRuntime *rt , struct ZlangObject *obj , size_t *summarized_obj_size , size_t *summarized_direct_prop_size )
{
	SUMMARIZE_SIZE( summarized_direct_prop_size , sizeof(struct ZlangDirectProperty_guimonitor) )
	return;
}

static struct ZlangDirectFunctions direct_funcs_guimonitor =
	{
		ZLANG_OBJECT_guimonitor , /* char *ancestor_name */
		
		ZlangCreateDirectProperty_guimonitor , /* ZlangCreateDirectPropertyFunction *create_entity_func */
		ZlangDestroyDirectProperty_guimonitor , /* ZlangDestroyDirectPropertyFunction *destroy_entity_func */
		
		NULL , /* ZlangFromCharPtrFunction *from_char_ptr_func */
		NULL , /* ZlangToStringFunction *to_string_func */
		NULL , /* ZlangFromDataPtrFunction *from_data_ptr_func */
		NULL , /* ZlangGetDataPtrFunction *get_data_ptr_func */
		
		NULL , /* ZlangOperatorFunction *oper_PLUS_func */
		NULL , /* ZlangOperatorFunction *oper_MINUS_func */
		NULL , /* ZlangOperatorFunction *oper_MUL_func */
		NULL , /* ZlangOperatorFunction *oper_DIV_func */
		NULL , /* ZlangOperatorFunction *oper_MOD_func */
		
		NULL , /* ZlangUnaryOperatorFunction *unaryoper_NEGATIVE_func */
		NULL , /* ZlangUnaryOperatorFunction *unaryoper_NOT_func */
		NULL , /* ZlangUnaryOperatorFunction *unaryoper_BIT_REVERSE_func */
		NULL , /* ZlangUnaryOperatorFunction *unaryoper_PLUS_PLUS_func */
		NULL , /* ZlangUnaryOperatorFunction *unaryoper_MINUS_MINUS_func */
		
		NULL , /* ZlangCompareFunction *comp_EGUAL_func */
		NULL , /* ZlangCompareFunction *comp_NOTEGUAL_func */
		NULL , /* ZlangCompareFunction *comp_LT_func */
		NULL , /* ZlangCompareFunction *comp_LE_func */
		NULL , /* ZlangCompareFunction *comp_GT_func */
		NULL , /* ZlangCompareFunction *comp_GE_func */
		
		NULL , /* ZlangLogicFunction *logic_AND_func */
		NULL , /* ZlangLogicFunction *logic_OR_func */
		
		NULL , /* ZlangLogicFunction *bit_AND_func */
		NULL , /* ZlangLogicFunction *bit_XOR_func */
		NULL , /* ZlangLogicFunction *bit_OR_func */
		NULL , /* ZlangLogicFunction *bit_MOVELEFT_func */
		NULL , /* ZlangLogicFunction *bit_MOVERIGHT_func */
		
		ZlangSummarizeDirectPropertySize_guimonitor , /* ZlangSummarizeDirectPropertySizeFunction *summarize_direct_prop_size_func */
	} ;

ZlangImportObjectFunction ZlangImportObject_guimonitor;
struct ZlangObject *ZlangImportObject_guimonitor( struct ZlangRuntime *rt )
{
	struct ZlangObject	*obj = NULL ;
	struct ZlangFunction	*func = NULL ;
	int			nret = 0 ;
	
	nret = ImportObject( rt , & obj , ZLANG_OBJECT_guimonitor , & direct_funcs_guimonitor , sizeof(struct ZlangDirectFunctions) , NULL ) ;
	if( nret )
	{
		SET_RUNTIME_ERROR( rt , RUNTIME_ERROR , ZLANG_ERROR_LINK_FUNC_TO_ENTITY , "import object to global objects heap" )
		return NULL;
	}
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_guimonitor_GetScreenWidth , ZLANG_OBJECT_int , "GetScreenWidth" , NULL ) ;
	if( func == NULL )
		return NULL;
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_guimonitor_GetScreenHeight , ZLANG_OBJECT_int , "GetScreenHeight" , NULL ) ;
	if( func == NULL )
		return NULL;
	
#if 0
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_guimonitor_GetWorkareaWidth , ZLANG_OBJECT_int , "GetWorkareaWidth" , NULL ) ;
	if( func == NULL )
		return NULL;
#endif
	
	return obj ;
}

